|
1
|
|
|
import React, { Component } from 'react' |
|
2
|
|
|
import { Redirect } from 'react-router' |
|
3
|
|
|
import Bar from './bar' |
|
4
|
|
|
|
|
5
|
|
|
export default class Visualizer extends Component { |
|
6
|
|
|
constructor (props) { |
|
7
|
|
|
super(props) |
|
8
|
|
|
|
|
9
|
|
|
this.state = { |
|
10
|
|
|
history: [], |
|
11
|
|
|
numbers: [], |
|
12
|
|
|
active: [], |
|
13
|
|
|
size: 0, |
|
14
|
|
|
scalingFactor: Math.floor(window.innerHeight) * 0.8, |
|
15
|
|
|
paused: false, |
|
16
|
|
|
finised: false, |
|
17
|
|
|
redirect: false, |
|
18
|
|
|
nextUrl: "" |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
this.speed = this.props.speed || 2 |
|
24
|
|
|
this.index = 0 |
|
25
|
|
|
this.algorithm = this.props.algorithm |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
init () {} |
|
29
|
|
|
|
|
30
|
|
|
componentDidMount () { |
|
31
|
|
|
this.listener = window.addEventListener('keydown', e => { |
|
32
|
|
|
if (e.key === 'f') { |
|
33
|
|
|
this.stop() |
|
34
|
|
|
this.setState({ |
|
35
|
|
|
nextUrl: this.props.next |
|
36
|
|
|
}) |
|
37
|
|
|
} else if (e.key === 'b') { |
|
38
|
|
|
this.stop() |
|
39
|
|
|
this.setState({ |
|
40
|
|
|
nextUrl: this.props.prev |
|
41
|
|
|
}) |
|
42
|
|
|
} |
|
43
|
|
|
}) |
|
44
|
|
|
|
|
45
|
|
|
let amount = 100 |
|
46
|
|
|
|
|
47
|
|
|
let randomNumbers = [...Array(amount).keys()].map(x => Math.random()) |
|
48
|
|
|
|
|
49
|
|
|
window.addEventListener('resize', () => { |
|
50
|
|
|
this.setState({ |
|
51
|
|
|
size: window.innerWidth / amount, |
|
52
|
|
|
scalingFactor: Math.floor(window.innerHeight) * 0.8 |
|
53
|
|
|
}) |
|
54
|
|
|
}) |
|
55
|
|
|
|
|
56
|
|
|
window.addEventListener('orientationchange', () => { |
|
57
|
|
|
this.setState({ |
|
58
|
|
|
size: window.innerWidth / amount, |
|
59
|
|
|
scalingFactor: Math.floor(window.innerHeight) * 0.8 |
|
60
|
|
|
}) |
|
61
|
|
|
}) |
|
62
|
|
|
|
|
63
|
|
|
this.setState({ |
|
64
|
|
|
size: this.props.size || window.innerWidth / amount |
|
65
|
|
|
}) |
|
66
|
|
|
|
|
67
|
|
|
this.setState({ |
|
68
|
|
|
history: this.algorithm(randomNumbers) |
|
69
|
|
|
}) |
|
70
|
|
|
|
|
71
|
|
|
this.interval = setInterval(() => { |
|
72
|
|
|
if (!this.state.paused) { |
|
73
|
|
|
this.index++ >= this.state.history.length |
|
74
|
|
|
? this.stop() |
|
75
|
|
|
: this.setState(this.state.history[this.index]) |
|
76
|
|
|
} |
|
77
|
|
|
}, 10) |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
componentWillUnmount () { |
|
81
|
|
|
clearInterval(this.interval) |
|
82
|
|
|
// this.setState({}) |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
start () {} |
|
86
|
|
|
|
|
87
|
|
|
stop () { |
|
88
|
|
|
clearInterval(this.interval) |
|
89
|
|
|
this.setState({ |
|
90
|
|
|
redirect: true |
|
91
|
|
|
}) |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
render () { |
|
95
|
|
|
if (this.state.redirect) { |
|
96
|
|
|
return <Redirect to={this.state.nextUrl || this.props.next} /> |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return ( |
|
100
|
|
|
<div |
|
101
|
|
|
className='App' |
|
102
|
|
|
style={{ |
|
103
|
|
|
margin: '0 auto', |
|
104
|
|
|
position: 'absolute', |
|
105
|
|
|
width: '100%', |
|
106
|
|
|
height: '100%' |
|
107
|
|
|
}} |
|
108
|
|
|
> |
|
109
|
|
|
{/* <h1 style={{color: 'tomato'}}>Hello</h1> */} |
|
110
|
|
|
|
|
111
|
|
|
<div style={{ position: 'relative', margin: '0 auto' }}> |
|
112
|
|
|
<svg width={`${window.innerWidth}px`} height={`100vh`}> |
|
113
|
|
|
{this.state.numbers.map((x, index) => { |
|
114
|
|
|
let width = this.state.size |
|
115
|
|
|
|
|
116
|
|
|
const data = { |
|
117
|
|
|
index, |
|
118
|
|
|
width, |
|
119
|
|
|
value: x * this.state.scalingFactor, |
|
120
|
|
|
fill: this.state.active.includes(index) ? '#FE6A6A' : '#72D7D1' |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return <Bar key={index} data={data} /> |
|
124
|
|
|
})} |
|
125
|
|
|
</svg> |
|
126
|
|
|
</div> |
|
127
|
|
|
</div> |
|
128
|
|
|
) |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|